<?php
/**
  * 会员模型
  */
class UsersModel extends CommonModel
{
	protected $_validate=array(
		array('username','require','用户名必须！',self::MODEL_BOTH),
		array('username','','用户名已经存在',0,'unique',1)
		);

	protected $_auto=array(
		array('add_time','time',self::MODEL_INSERT,'function'),
		array('password','md5',self::MODEL_INSERT,'function'),
		array('nickname','callbackNickname',self::MODEL_INSERT,'callback'),
		array('status','callbackStatus',self::MODEL_UPDATE,'callback'),
		array('birthday','callbackBirthday',self::MODEL_BOTH,'callback')		
		);

	protected function callbackNickname()
	{
	  return $_POST['username'];
	}

	protected function callbackBirthday()
	{
	  if(isset($_POST['birthday']))
	  {
	  	return strtotime($_POST['birthday']);
	  }else
	  		{
	  			return '0';
	  		}
	}

	/**
	* 账户资金变动
	* @param int $uid 用户id
	* @param int $money 变动资金
	* @param int $payPoints 变动积分
	* @param string $desc 变动原因
	* @param int $type 变动类型:1-后台管理员操作
	* @param 
	*/
	public function account($uid,$money,$payPoints,$desc,$type)
	{
		$currentData=$this->field('user_money,pay_points,id')->find($uid);
		$currentData['user_money']=$currentData['user_money']+$money;
		$currentData['pay_points']=$currentData['pay_points']+$payPoints;
		if($this->save($currentData)!==FALSE)
		{
			$log['user_id']=$currentData['id'];
			$log['user_money']=$money;
			$log['pay_points']=$payPoints;
			$log['change_time']=time();
			$log['change_desc']=$desc;
			$log['change_type']=$type;
			if(M('AccountLog')->add($log)!==FALSE)
			{
				return true;
			}
		}
		
	}		

}
?>